import util,cv2,time,random,config,logger,sys
import yys_operator as yo
def personEnchant(count):
    data=getCompareImage(config.pe_other)
    i=1
    if count<=0:
        count=99999
    while(i<=count):
        logger.info('第'+str(i)+"轮个人结界")
        loopPersonEnchant(data)
        i+=1
def loopPersonEnchant(data):
    is_end=0
    while(is_end<9):
        #预防弹出悬赏
        tmp=yo.get_screen()
        loopPeClick(data,config.pe_other,1)
        # logger.info(1111)  
        if yo.match(data['pe_record']['data'],tmp)==1:
            #在个人结界上的操作
            attack=config.pe_attack
            atta=attack[is_end]
            # logger.info(atta)
            handClick(atta['ax'],atta['ay'])
            yo.get_randtime(atta['atime'],atta['atime']+1)
            tmp=yo.get_screen()
            if yo.match(data['pe_attack']['data'],tmp)==1:
                handClick(atta['x'],atta['y'])
                logger.info("第"+str(is_end+1)+"个结界开始攻击") 
                yo.get_randtime(atta['time'],atta['time']+1)
                loopPeClick(data,config.pe_other)
                # logger.info(2222)
        
            else:
                logger.info("第"+str(is_end+1)+"个结界已经攻击过")
            is_end+=1    
            if is_end>=9:       
                doPeAfterFinish(getCompareImage(config.pe_finish),config.pe_finish)        
                        
def loopPeClick(data,conf,is_end=0):
    # logger.info(data)
    re=loopPeModClick(data,conf) 
    if re<1 and is_end==0:
        yo.get_randtime(5,6)
        while(re<1):            
            re=loopPeModClick(data,conf)
            # logger.info(re)
            if re==0:          
                yo.get_randtime(5,6)
    elif re==0:
        yo.get_randtime(5,6)    
def doPeAfterFinish(data,conf):
    i=0
    while(i<1):
        tmp=yo.get_screen()
        logger.info("刷新页面")
        re=checkAndClick(data['pe_confirm'],conf['pe_confirm'],tmp)
        if re==1:
            return 1
        checkAndClick(data['pe_refresh'],conf['pe_refresh'],tmp)
        yo.get_randtime(1,2)    
def checkAndClick(data,efig,tmp):
    if(yo.match(data['data'],tmp)==1):
        handClick(efig['x'],efig['y'])    
        yo.get_randtime(efig['time'],efig['time']+2)
        return 1
    return 0                    
def loopPeModClick(data,conf):
    tmp=yo.get_screen()
    logger.info("刷新页面")
    is_end=0
    for event in data:
         ent=data[event]
         if ent['is_click']==0:
            continue 
         re=yo.match(ent['data'],tmp)
         if re==1: 
            # logger.info(conf) 
            efig=conf[ent['name']]
            logger.info(efig['name']+"匹配")
            if not efig:
                continue
            if 'is_end' in efig and efig['is_end']==1:
                is_end+=1
            #等待一秒多再点击    
            handClick(efig['x'],efig['y'])    
            #点击后需要停顿时间
            yo.get_randtime(efig['time'],efig['time']+2)
    if is_end>0:
         return 1
    else:
         return 0            
def getCompareImage(conf):
    data={}
    for event in conf:
        if 'file' in conf[event]:
            imgData=getImgData(conf[event]['file'])
        else:
            imgData=getImgData(event)
        if 'is_click' in conf[event] and conf[event]['is_click']==0:
            is_click=0
        else:
            is_click=1        
        data[event]={'name':event,'data':imgData,'is_click':is_click}
    return data
def getImgData(imgName):
    imgPath=config.imgPath
    file=''
    if '.' in imgName:
        file=imgPath+imgName
    else:
        file=imgPath+imgName+'.png'
    return cv2.imread(file,0)                 
def handClick(x,y):
     yo.get_randtime(1,2)  
     sx, sy = yo.get_randxy(x,y)
     yo.click(sx, sy)